package com.txy.springcloud.controller;

import com.txy.springcloud.client.TxyServiceCodeFeignClient;
import com.txy.springcloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TxyServiceCodeFeignClient codeClient;


    @PostMapping("/register")
    public String regist(
            HttpServletResponse response,
            @RequestParam("email") String email,
            @RequestParam("password") String password,
            @RequestParam("code") String verificationCode) {
        try {
            if (codeClient.validate(email, verificationCode) == 0) {
                String token = UUID.randomUUID().toString();
                response.addCookie(new Cookie("token", token));
                if(userService.insert(email, password, token)) {
                    return token;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return "";
    }

    // curl -X POST "http://localhost:8080/user/2389679202@qq.com/sendCode"
    @PostMapping("/{email}/sendCode")
    public boolean sendCode(@PathVariable("email") String email) {
        try {
            return codeClient.create(email);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @GetMapping("/isRegistered")
    public boolean isRegistered(
            @RequestParam("email") String email
    ) {
        try {
            return userService.isRegistered(email);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // curl -X POST "http://127.0.0.1:8080/user/login?email=abc@com&password=abc"
    @PostMapping("/login")
    public String login(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("email") String email,
            @RequestParam("password") String password
    ) {
        try {
            String token = UUID.randomUUID().toString();
            response.addCookie(new Cookie("token", token));
            boolean checkLogin = userService.checkPassword(email, password);
            if (checkLogin) {
                 userService.setToken(email, token);
                 return token;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return "";
    }

    // curl "http://www.txy.com:9003/api/user/info/test-token"
    // curl "http://127.0.0.1:8080/user/info/test-token"
    @GetMapping("/info/{token}")
    public String getEmailById(@PathVariable("token") String token) {
        try {
            return userService.getEmail(token);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
